
import 'package:factoryside/api/api.dart';
import 'package:factoryside/utils/ColorsUtils.dart';
import 'package:factoryside/utils/request_util.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
///催单,取消工单,放弃返还旧件，终止
class UrgePage extends StatefulWidget{

  int id;
  int type;//0催单 1取消工单 2放弃返还旧件 3终止

  UrgePage(this.id,this.type);

  @override
  State<StatefulWidget> createState() {
    return UrgePageState();
  }
}

class UrgePageState extends State<UrgePage>{

  TextEditingController msgCon=TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:buildAppBar(getTitle(),null,context),
      //未获取到数据就居中显示加载图标
      body: buildBody(context),
    );
  }

  String getTitle(){
    if(widget.type==0){
      return '催单';
    }else if(widget.type==1){
      return '取消工单';
    }else if(widget.type==2){
      return '放弃返还旧件';
    }else if(widget.type==3){
      return '终止';
    } else{
      return '';
    }
  }
  String getHint(){
    if(widget.type==0){
      return '西瓜鱼客服被催单过多会影响业绩，感谢您的理解和支持!';
    }else if(widget.type==1){
      return '请输入取消理由';
    }else if(widget.type==2){
      return '请输入原因';
    }else if(widget.type==3){
      return '请输入原因';
    }else{
      return '';
    }
  }
  String getUrl(){
    if(widget.type==0){
      return Api.reminders;
    }else if(widget.type==1){
      return Api.cancelOrder;
    }else if(widget.type==2){
      return Api.giveUp;
    }else if(widget.type==3){
      return Api.newStopAccessory;
    } else{
      return '';
    }
  }
  dynamic getParams(){
    if(widget.type==0||widget.type==1){
      return {
        'OrderId':'${widget.id}',
        'Remark': msgCon.text
      };
    }else if(widget.type==2){
      return {
        'AccessoryID':'${widget.id}',
        'GiveUpRemarks': msgCon.text
      };
    }else if(widget.type==3){
      return {
        'AccessoryID':'${widget.id}',
        'Remark': msgCon.text
      };
    } else{
      return {};
    }
  }

  Widget buildBody(BuildContext context){
    return ListView(
      children: [
        /*输入框*/
        Container(
            margin: EdgeInsets.only(
                top: 10, bottom: 0, left: 10, right: 10),
            padding: EdgeInsets.only(bottom: 5, left: 10, right: 10),
            width: double.infinity,
            decoration: BoxDecoration(
                color: Color.fromARGB(180, 240, 240, 240),
                border: Border.all(width: 0.2, color: Colors.grey),
                borderRadius: BorderRadius.all(Radius.circular(3))),
            child: TextField(
              controller: msgCon,
              style: TextStyle(fontSize: 13, color: Colors.black),
              maxLines: 4,
//              keyboardType: TextInputType.text,
              decoration: InputDecoration(
                hintText: getHint(),
                counterText: '',
                disabledBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
              ),
            )),
        Container(
          color: Colors.transparent,
          width: double.infinity,
          height: 50,
          margin: EdgeInsets.all(10),
          child: RaisedButton(
            onPressed: () {
              if(isFastClick())return;//防重复点击
                if (msgCon.text.isEmpty) {
                  showToast( '内容不能为空');
                  return;
                }
                RequestUtil.post(getUrl(),
                    getParams()
                ).then((value) => this.setState(() {
                  if(value['Data']['code']==0){
                    Navigator.pop(context,true);
                    showToast('操作成功');
                  }else{
                    showToast(value['Data']['msg']);
                  }

                }));
            },
            child: Text(
              '提交',
              style: TextStyle(color: Colors.white),
            ),
            color: ColorsUtil.hexToColor(Api.btnColor),
          ),
        )
      ],
    );
  }

  Widget showLoading(){
    return Center(
      child: CircularProgressIndicator(),
    );
  }

}
